﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ResearchRepository.Models;

namespace WebControlsTest
{
	public static partial class GLOBALS
	{
		// session variable names
		public const string AdminReqListSessionVar = "AdminReqList";
	}
    public partial class RRAdmin : System.Web.UI.Page
    {
////////
////////	properties
////////
		protected List<RRRequest> requestList
		{
			get
			{
				return (List<RRRequest>) Session[GLOBALS.AdminReqListSessionVar];
			}
			set
			{
				Session[GLOBALS.AdminReqListSessionVar] = value;
			}
		}
////////
////////	Page Events
////////
        protected void Page_Load(object sender, EventArgs e)
        {
			Title = GLOBALS.masterTitlePrefix + " - Admin";
            // if not logged in, then redirect to register page
            if (Session[GLOBALS.userObjSessionVar] == null)
            {
                Response.Redirect("Login.aspx");
            }
			// if not an admin, then redirect to browse page (TODO: redirect to requests section of accounts page)
			else if (!RRDatabaseManager.userIsAdmin(((RRUser) Session[GLOBALS.userObjSessionVar]).UserID))
			{
                Response.Redirect("Browse.aspx");
			}
			// else (logged in as admin): initialize request grid on non-postback
			else if (!IsPostBack)
			{
				populateRequestGrid();
			}
        }
////////
////////	Button Events
////////
        protected void approveRequestButton_Click(object sender, EventArgs e)
        {
			RRRequest req = requestList[requestGrid.SelectedIndex];
			try
			{
				req.Note = commentsBox.Text;
				req.AdminID = ((RRUser) Session[GLOBALS.userObjSessionVar]).UserID;
				RRDatabaseManager.acceptRequest(req);
				statusLabel.Text = "Request #" + req.ReqID + " accepted.";
			}
			catch(Exception x)
			{
				statusLabel.Text = "Unable to accept request #" + req.ReqID + " due to server error: " + x.Message;
			}
			populateRequestGrid();
        }
        protected void rejectRequestButton_Click(object sender, EventArgs e)
        {
			RRRequest req = requestList[requestGrid.SelectedIndex];
			try
			{
				req.Note = commentsBox.Text;
				req.AdminID = ((RRUser) Session[GLOBALS.userObjSessionVar]).UserID;
				RRDatabaseManager.rejectRequest(req);
				statusLabel.Text = "Request #" + req.ReqID + " rejected.";
			}
			catch(Exception x)
			{
				statusLabel.Text = "Unable to reject request #" + req.ReqID + " due to server error: " + x.Message;
			}
			populateRequestGrid();
        }
		protected void refreshListButton_Click(object sender, EventArgs e)
		{
			populateRequestGrid();
		}
////////
////////	GridView Events
////////
		// adds onClick event to data rows, to allow selection and directory opening
		public void gridRowDataBound(object sender, GridViewRowEventArgs e)
		{
			GridViewRow r = e.Row;
			if (r.RowType == DataControlRowType.Header)// || r.RowType == DataControlRowType.EmptyDataRow)
			{
				//TODO??
				return;
			}
			if (r.RowType != DataControlRowType.DataRow)
			{
				//TODO??
				return;
			}
			// on click, select row and copy data to edit fields
			r.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference((GridView) sender, "Select$"+r.RowIndex));
		}
		// update context-sensitive interface controls
		public void gridRowSelected(object sender, EventArgs e)
		{
			GridView grid = (GridView) sender;
			RRRequest req = requestList[grid.SelectedIndex];
			commentsBox.Text = req.Note;
			approveRequestButton.Enabled = true;
			rejectRequestButton.Enabled = true;
		}
////////
////////	Util Functions
////////
		protected void populateRequestGrid()
		{
			try
			{
				deselectGridRows();
				requestList = new List<RRRequest>(RRDatabaseManager.getAdminRequestList(((RRUser) Session[GLOBALS.userObjSessionVar]).UserID));
				requestGrid.DataSource = requestList;
				requestGrid.DataBind();
			}
			catch(Exception x)
			{
				statusLabel.Text = "Unable to populate request list: " + x.Message;
			}
		}
		public void deselectGridRows()
		{
			requestGrid.SelectedIndex = -1;
			commentsBox.Text = "";
			approveRequestButton.Enabled = false;
			rejectRequestButton.Enabled = false;
			//statusLabel.Text = "Select a request.";
		}
    }
}